package com.yy.bolg.util;

import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 时间工具类
 * @author enmaoFu
 * @date 2024-02-29
 */
public class DateUtil {

    /**
     * 获取当前系统时间
     * 年-月-日 时:分:秒
     * @return
     */
    public static String getSystemDate(){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = sdf.format(date);
        System.out.println("时间输出：" + formattedDate);
        return formattedDate.toString();
    }

    /**
     * 获取当前系统时间-自定义格式
     * @return
     */
    public static String getCustomizedSystemDate(String pattern){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String formattedDate = sdf.format(date);
        System.out.println("时间输出：" + formattedDate);
        return formattedDate.toString();
    }

    /**
     * 计算两个时间直接相差月数
     * @param startDate
     * @param endDate
     * @return
     * @throws ParseException
     */
    public static int getMonthDiff(String startDate, String endDate) throws ParseException {
        int monthday;
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM");
        Date startDate1 = fmt.parse(startDate);
        Calendar starCal = Calendar.getInstance();
        starCal.setTime(startDate1);
        int sYear = starCal.get(Calendar.YEAR);
        int sMonth = starCal.get(Calendar.MONTH);
        int sDay = starCal.get(Calendar.DAY_OF_MONTH);
        Date endDate1 = fmt.parse(endDate);
        Calendar endCal = Calendar.getInstance();
        endCal.setTime(endDate1);
        int eYear = endCal.get(Calendar.YEAR);
        int eMonth = endCal.get(Calendar.MONTH);
        int eDay = endCal.get(Calendar.DAY_OF_MONTH);
        monthday = ((eYear - sYear) * 12 + (eMonth - sMonth));
        //这里计算零头的情况，根据实际确定是否要加1 还是要减1
        if (sDay < eDay) {
            monthday = monthday + 1;
        }
        return monthday;
    }

    /**
     * 增加月份
     * @param date
     * @param i
     * @return
     * @throws ParseException
     */
    public static String monthAdd(String date,int i) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM");
        Date startDate1 = fmt.parse(date);
        calendar.setTime(startDate1);
        calendar.add(Calendar.MONTH, i);
        return fmt.format(calendar.getTime());
    }

    /**
     * 判断选择的日期是否是今天
     * @param time
     * @return
     */
    public static boolean isToday(Date time) {
        return isThisTime(time, "yyyy-MM-dd");
    }

    /**
     * 判断选择的日期是否是本月
     * @param time
     * @return
     */
    public static boolean isThisMonth(Date time) {
        return isThisTime(time, "yyyy-MM");
    }

    /**
     * 判断选择的日期是否是本年
     * @param time
     * @return
     */
    public static boolean isThisYear(Date time) {
        return isThisTime(time, "yyyy");
    }

    /**
     * 字符串转换成日期
     * @param str
     * @param pattern yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static Date strToDate(String str, String pattern) {

        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    private static boolean isThisTime(Date time, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String param = sdf.format(time);//参数时间
        String now = sdf.format(new Date());//当前时间
        if (param.equals(now)) {
            return true;
        }
        return false;
    }

    /**
     * 获取近7天日期（含今天）
     * @return
     */
    public static List<String> getSevenDate() {
        List<String> dateList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < 7; i++) {
            Date date = DateUtils.addDays(new Date(), -i);
            String formatDate = sdf.format(date);
            dateList.add(formatDate);
        }
        return dateList;
    }

}
